//
//  PxHomeViewController.m
//  OilCard
//
//  Created by Xu Yefeng on 14-5-18.
//  Copyright (c) 2014年 Free. All rights reserved.
//

#import "PxHomeViewController.h"


@implementation PxHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self setLeftBack];
    [super setLogoTitleViewWithTitle:@"极配"];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    lineView.backgroundColor = [UIColor colorWithHTMLStr:@"febb9f"];
    lineView.layer.opacity = 0.6;
    [self.view addSubview:lineView];
    
    CGRect frame = CGRectMake(0, 1, self.view.width, [PxGlobal getCustomerHeight:135]);
    UIView *backView = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:backView];
    UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/index-topBg"]];
    [backView setBackgroundColor:bgColor];
    
    UIButton *btn = [PxWidgetCreater customerButton];
    btn.frame = frame;
    [btn setImage:[UIImage imageNamed:@"Images/index-header"]];
    [btn setTitle:[PxNetSession sharedSession].currentUser.phone];
//    CGFloat offset = [PxGlobal getCustomerHeight:25] - btn.imageView.top - btn.imageView.height/2;
//    CGFloat offset2 = frame.size.width/2 - btn.imageView.width/2 - btn.imageView.left + btn.imageView.width;
    btn.imageEdgeInsets = UIEdgeInsetsMake(-42.5, 110, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake([PxGlobal getCustomerHeight:42.5], -[PxGlobal getCustomerWidth:55], 0, 0);
    [backView addSubview:btn];
    [btn setSelector:@selector(gotoUserCenter) withTarget:self];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scroll.top = backView.bottom;
    scroll.height = CGRectGetHeight(self.view.frame) - [PxGlobal getBaseHeight:NO] - btn.height;
    scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:scroll];
    self.contentView = scroll;
    
//    [self setRightBtn];
    [self initHome];
    [self getCompany];
    [self getSMSModel];
//    [self checkVersion];
}

-(void)getCompany
{
    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
    cmd.functionName = TRANS_STORE_INFO;
    
    [cmd.paramDic addEntriesFromDictionary:@{@"type":@"expressCompany"}];
    
    cmd.businessSuccessHandler = ^(NSDictionary* dic)
    {
        NSMutableArray *compArrays = [NSMutableArray arrayWithArray:dic[RESULT_DATA]];
        [compArrays insertObject:@{@"label":@"请选择", @"value":@""} atIndex:0];
        [PxGlobal saveData:compArrays andKey:COMPANY_INFO];
    };
    
    cmd.businessErrorHandler = ^(NSDictionary* dic)
    {
        [LLAlertService alertWithTip:dic[RESULT_DESC]];
    };
    
    cmd.errorHandler = ^(NSError* error)
    {
        
    };
    
    [LLRestEngine sendCommandRequest:cmd];
}

-(void)getSMSModel
{
    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
    cmd.functionName = TRANS_SMSMODEL_INFO;
    
//    [cmd.paramDic addEntriesFromDictionary:@{@"type":@"expressCompany"}];
    
    cmd.businessSuccessHandler = ^(NSDictionary* dic)
    {
        NSMutableArray *smsModelArrays = [NSMutableArray arrayWithArray:dic[RESULT_DATA]];
//        [compArrays insertObject:@{@"label":@"请选择", @"value":@""} atIndex:0];
        [PxGlobal saveData:smsModelArrays andKey:SMSMODEL_INFO];
    };
    
    cmd.businessErrorHandler = ^(NSDictionary* dic)
    {
        [LLAlertService alertWithTip:dic[RESULT_DESC]];
    };
    
    cmd.errorHandler = ^(NSError* error)
    {
        
    };
    
    [LLRestEngine sendCommandRequest:cmd];
}

-(void)setRightBtn
{
    UIButton *navBarRightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 47, 29)];
//    navBarRightButton.backgroundColor = [UIColor redColor];
    [navBarRightButton addTarget:self action:@selector(navBarRightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [navBarRightButton setImage:[UIImage imageNamed:@"Images/index-user"]];
    [navBarRightButton setContentMode:UIViewContentModeCenter];
    //        self.navBarRightButton.showsTouchWhenHighlighted = YES;
    
//    self.navBarRightButton.titleLabel.font = [UIFont systemFontOfSize:17];
//    UIColor *color1 = [UIColor colorWithHTMLStr:@"FFFFFF"];
//    [self.navBarRightButton setTitleColor:color1 forState:UIControlStateNormal];
    
    UIBarButtonItem *rightButtonItem=[[UIBarButtonItem alloc] initWithCustomView:navBarRightButton];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    if([LLYTSmallUtil iOSBigVersion] >= 6.5)
        negativeSpacer.width = -9;// it was -6 in iOS 6
    else
        negativeSpacer.width = 1;
    
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,rightButtonItem/*this will be the button which u actually need*/, nil] animated:NO];
}

- (void)navBarRightBtnAction
{
    [self pushViewController:@"PxUserHomeViewController" withIncomingParam:nil];
}

-(void)initHome
{
    CGFloat height = [PxGlobal getCustomerHeight:125];
    
    CGRect frame = CGRectMake(0, 0, self.view.width/2, height);
    
    UIButton *btn1 = [self getHomeBtn];
    btn1.tag = 1001;
    [btn1 setSelector:@selector(gotoPage:) withTarget:self];
    [btn1 setImage:[UIImage imageNamed:@"Images/index-img01"]];
    [btn1 setTitle:@"学校入库"];
    btn1.frame = frame;
    [self.contentView addSubview:btn1];
    
    UIButton *btn2 = [self getHomeBtn];
    btn2.tag = 1002;
    [btn2 setSelector:@selector(gotoPage:) withTarget:self];
    [btn2 setImage:[UIImage imageNamed:@"Images/index-img02"]];
    [btn2 setTitle:@"小区入库"];
    btn2.frame = frame;
    btn2.left = btn1.right;
    [self.contentView addSubview:btn2];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, btn2.bottom, self.view.width, 1)];
    lineView.backgroundColor = [UIColor colorWithHTMLStr:@"d0d0d0"];
    [self.contentView addSubview:lineView];
    
    CGFloat top = btn2.bottom + 1;
    
    UIButton *btn3 = [self getHomeBtn];
    btn3.tag = 1003;
    [btn3 setSelector:@selector(gotoPage:) withTarget:self];
    [btn3 setImage:[UIImage imageNamed:@"Images/index-img03"]];
    [btn3 setTitle:@"短信通知"];
    btn3.frame = frame;
    btn3.top = top;
    [self.contentView addSubview:btn3];
    
    UIButton *btn4 = [self getHomeBtn];
    btn4.tag = 1004;
    [btn4 setSelector:@selector(gotoPage:) withTarget:self];
    [btn4 setImage:[UIImage imageNamed:@"Images/index-img04"]];
    [btn4 setTitle:@"电话通知"];
    btn4.frame = frame;
    btn4.top = top;
    btn4.left = btn3.right;
    [self.contentView addSubview:btn4];
    
    top = btn4.bottom;
    
    UIView *lineView2 = [PxGlobal duplicate:lineView];
    lineView2.top = top;
    [self.contentView addSubview:lineView2];
    
    UIView *lineView3 = [[UIView alloc] initWithFrame:CGRectMake(self.view.width/2, 0, 1, frame.size.height*2 + 1)];
    lineView3.backgroundColor = [UIColor colorWithHTMLStr:@"d0d0d0"];
    [self.contentView addSubview:lineView3];
    
//    UIButton *btn5 = [PxWidgetCreater customerButton];
//    btn5.tag = 1005;
//    [btn5 setSelector:@selector(gotoPage:) withTarget:self];
//    [btn5 setImage:[UIImage imageNamed:@"Images/index-icon05"]];
//    btn5.frame = frameLeft;
//    btn5.top = top;
//    [self.contentView addSubview:btn5];
//    
//    UIButton *btn6 = [PxWidgetCreater customerButton];
//    btn6.tag = 1006;
//    [btn6 setSelector:@selector(gotoPage:) withTarget:self];
//    [btn6 setImage:[UIImage imageNamed:@"Images/index-icon06"]];
//    btn6.frame = frameRight;
//    btn6.top = top;
//    [self.contentView addSubview:btn6];
    
    self.contentView.contentSize = CGSizeMake(self.contentView.width, top + 1);
}

-(UIButton *)getHomeBtn
{
    UIButton *btn = [PxWidgetCreater customerButton];
//    btn.layer.borderColor = [UIColor colorWithHTMLStr:@"d0d0d0"].CGColor;
//    btn.layer.borderWidth = 1;
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn setTitleColor:[UIColor colorWithHTMLStr:@"959595"] forState:UIControlStateNormal];

    btn.imageEdgeInsets = UIEdgeInsetsMake(-30, 46, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake([PxGlobal getCustomerHeight:60], -[PxGlobal getCustomerWidth:58], 0, 0);
    
    return btn;
}

-(void)gotoUserCenter
{
    [self pushViewController:@"PxUserHomeViewController" withIncomingParam:nil];
}

-(void)gotoPage:(UIButton *)button
{
    NSInteger tag = button.tag;
    LLDLog(@"%li", (long)tag);
    switch (tag) {
        case 1001:
            [self pushViewController:@"PxWayBillNotifyViewController" withIncomingParam:@{@"companyCode":[PxGlobal getData:COMPANY_CODE]==nil?@"":[PxGlobal getData:COMPANY_CODE],
                                                                                          @"expressCompany":[PxGlobal getData:COMPANY_NAME]==nil?@"":[PxGlobal getData:COMPANY_NAME]}];
            break;
        case 1002:
            [self pushViewController:@"PxAVCaptureViewController" withIncomingParam:@{@"type":@"1",
                                                                                      @"title":@"扫描分区"}];
            break;
        case 1003:
            [self pushViewController:@"PxSMSNotifyViewController" withIncomingParam:nil];
            break;
        case 1004:
            [self pushViewController:@"PxScanCallViewController" withIncomingParam:nil];
            break;
        case 1005:
            [self pushViewController:@"PxWayBillQueryViewController" withIncomingParam:nil];
            break;
        case 1006:
            [self pushViewController:@"PxErrorWaybillListViewController" withIncomingParam:nil];
            break;
        default:
            break;
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self setLeftBack];
//    [super setLogoTitleViewWithTitle:@"极配"];
//    [UIView animateWithDuration:10.0 animations:^{
//        self.navigationController.navigationBar.backgroundColor = [[UIColor colorWithHTMLStr:@"ff4e08"] colorWithAlphaComponent:1];
//    } completion:^(BOOL finished) {
//        self.navigationController.navigationBar.backgroundColor = [[UIColor colorWithHTMLStr:@"ff4e08"] colorWithAlphaComponent:0];
//    }];
//    [self infoUpload];
}
//
//-(void)infoUpload
//{
//    FMResultSet *set = [PxGlobal selectTable:TABLE_APARTMENT withData:nil];
//    NSMutableArray *arrays = [NSMutableArray new];
//    while (set.next) {
//        NSMutableDictionary *dic = [PxGlobal getDicFromFMResultSet:set];
//        [dic setObject:dic[@"phone"] forKey:@"addresseePhone"];
//        [arrays addObject:dic];
//    }
//    if (arrays.count == 0)
//        return;
//    
//    PxBaseOilNetCmd *cmd = [PxBaseOilNetCmd cmd];
//    cmd.functionName = TRANS_WAYBILL_INFO_UPLOAD;
//    
//    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:arrays, @"waybills", nil];
//    
//    [cmd.paramDic addEntriesFromDictionary:data];
//    
//    cmd.businessSuccessHandler = ^(NSDictionary* dic)
//    {
//        [LLAlertService alertWithTip:@"通知已发送"];
//        for (NSDictionary *data in arrays) {
//            int selectId = [data[@"id"] intValue];
//            [PxGlobal deleteTabel:TABLE_APARTMENT withID:selectId];
//        }
//    };
//    
//    cmd.businessErrorHandler = ^(NSDictionary* dic)
//    {
//        
//        [LLAlertService sysAlertWithTip:dic[RESULT_DESC] withTitle:@"错误"];
//    };
//    
//    cmd.errorHandler = ^(NSError* error)
//    {
//        
//    };
//    
//    
//    [LLRestEngine sendCommandRequest:cmd];
//}

-(void)setLeftBack
{
//        UIColor *titleFontColor = [UIColor colorWithHTMLStr:@"000000"];
//        UIImage *img = [PxGlobal imageWithColor:[UIColor colorWithHTMLStr:@"ffffff"]];
    UIColor *titleFontColor = [UIColor colorWithHTMLStr:@"ffffff"];
    UIImage *img = [PxGlobal imageWithColor:[UIColor colorWithHTMLStr:@"ff4e08"]];
    if ([LLYTSmallUtil iOSBigVersion] >= 6.5) {
        
        [self.navigationController.navigationBar setBackgroundImage:img
                                                     forBarPosition:UIBarPositionTopAttached
                                                         barMetrics:UIBarMetricsDefault];
        
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : titleFontColor,
                                                                          NSFontAttributeName : [UIFont systemFontOfSize:20],
                                                                          }];
        
    }
    else
    {
        [self.navigationController.navigationBar setBackgroundImage:img
                                                      forBarMetrics:UIBarMetricsDefault];
        
        [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor : titleFontColor,
                                                                          UITextAttributeFont : [UIFont systemFontOfSize:20],
                                                                          UITextAttributeTextShadowColor: [UIColor clearColor],
                                                                          UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
                                                                          }];
        
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
